import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeIndex from '../pages/Home'
import LearnIndex from '../pages/Learn'
import LoginIndex from "../pages/Login";

Vue.use(VueRouter)

export default function () {
  const router = new VueRouter({
    routes: [
      {
        path: '/',
        name: 'HomeIndex',
        component: HomeIndex
      },
      {
        path: '/learn-index',
        name: 'LearnIndex',
        component: LearnIndex,
        //路由独享守卫
        // beforeEnter: (to, from, next) => {
        //   console.log(to)
        //   next()
        // },
        meta: {
          requiredAuth: true
        }
      },
      {
        path: '/login-index',
        name: 'LoginIndex',
        component: LoginIndex
      }
    ]
  })

  router.beforeEach((to, from, next) => {
    console.log(to)

    if(to.matched.some(item => item.meta.requiredAuth)) {
      //true  需要登录才可以访问，直接跳转到登录页面
      next({
        path: '/login-index'
      })

    } else {
      next()
    }
  })

  return router

}
